package wiki.xsx.core.bytecode.transformer;

import javassist.CtClass;
import lombok.SneakyThrows;
import wiki.xsx.core.bytecode.enums.ExecuteType;

import java.util.List;

/**
 * 方法转换器
 *
 * @author xsx
 * @date 2023/1/31
 * @since 1.8
 * <p>
 * Copyright (c) 2023 xsx All Rights Reserved.
 * easy-bytecode is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * </p>
 */
public class MethodTransformer extends AbstractMethodTransformer {

    /**
     * 有参构造
     *
     * @param isWrite        是否写入
     * @param fileDir        文件目录
     * @param executeType    执行类型
     * @param className      类名称
     * @param methodName     方法名称
     * @param parameterTypes 参数类型
     * @param sources        源码
     */
    public MethodTransformer(
            Boolean isWrite,
            String fileDir,
            ExecuteType executeType,
            String className,
            String methodName,
            List<Class<?>> parameterTypes,
            String... sources
    ) {
        this(isWrite, fileDir, executeType, className, methodName, parameterTypes, null, sources);
    }

    /**
     * 有参构造
     *
     * @param isWrite        是否写入
     * @param fileDir        文件目录
     * @param executeType    执行类型
     * @param className      类名称
     * @param methodName     方法名称
     * @param parameterTypes 参数类型
     * @param lineNum        源码行号
     * @param sources        源码
     */
    public MethodTransformer(
            Boolean isWrite,
            String fileDir,
            ExecuteType executeType,
            String className,
            String methodName,
            List<Class<?>> parameterTypes,
            Integer lineNum,
            String... sources
    ) {
        super(isWrite, fileDir, executeType, className, methodName, parameterTypes, lineNum, sources);
    }

    /**
     * 执行转换
     *
     * @param ctClass ct类
     * @return 返回字节码
     */
    @SneakyThrows
    @Override
    public byte[] execute(CtClass ctClass) {
        return this.processMethod(this.parameterTypes, ctClass, this::process);
    }
}
